﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class FormDanhSachKhuVuc : Form
    {
        public FormDanhSachKhuVuc()
        {
            InitializeComponent();

        }

        private void chkChonTatCa_CheckedChanged(object sender, EventArgs e)
        {
            if (chkChonTatCa.Checked)
            {
                foreach (DataGridViewRow r in gridViewXemDanhSachKhuVuc.Rows)
                {
                    r.Cells[3].Value = true;
                }
            }
            else if (!(chkChonTatCa.Checked))
            {
                foreach (DataGridViewRow r in gridViewXemDanhSachKhuVuc.Rows)
                {
                    r.Cells[3].Value = false;
                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FormDanhSachKhuVuc_Load(object sender, EventArgs e)
        {
            List<KhuVucDTO> danhsach = new List<KhuVucDTO>();
            KhuVucBUS bus = new KhuVucBUS();
            danhsach = bus.LayDanhSachKhuVuc();
            gridViewXemDanhSachKhuVuc.Rows.Clear();
            for (int i = 0; i < danhsach.Count; i++)
            {
                gridViewXemDanhSachKhuVuc.Rows.Add(i+1,danhsach[i].Id_Khu_Vuc, danhsach[i].Ten_Khu_Vuc);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult dialog = MessageBox.Show("Bạn Chắc Chắn Muốn Xóa Khu Vực Này?", "Xóa Khu Vực", MessageBoxButtons.YesNo);
            if (dialog == DialogResult.Yes)
            {
                List<DataGridViewRow> rowsToDelete = new List<DataGridViewRow>();
                foreach (DataGridViewRow row in gridViewXemDanhSachKhuVuc.Rows)
                {
                    DataGridViewCheckBoxCell chk = row.Cells[3] as DataGridViewCheckBoxCell;
                    if (Convert.ToBoolean(chk.Value) == true)
                        rowsToDelete.Add(row);
                }
                //loop through the list to delete rows added to list<T>:
                foreach (DataGridViewRow row in rowsToDelete)
                {
                    KhuVucBUS tk = new KhuVucBUS();
                    tk.XoaKhuVuc(Convert.ToInt32(row.Cells[1].Value));
                    gridViewXemDanhSachKhuVuc.Rows.Remove(row);
                }
            }
        }

        private void btnThemKhuVuc_Click(object sender, EventArgs e)
        {
            FormThemKhuVuc frm = new FormThemKhuVuc();
            frm.Show();
        }
        public static int ID;
        public static string Ten_Khu_Vuc = "";
        private void btnSua_Click(object sender, EventArgs e)
        {
            int chkDem = 0;
            foreach (DataGridViewRow singleRow in gridViewXemDanhSachKhuVuc.Rows)
            {
                if (singleRow.Cells[3].Value != null)
                {
                    if ((Boolean)singleRow.Cells[3].Value == true)
                    {
                        chkDem = chkDem + 1;
                    }
                }
            }
            if (chkDem == 0 || chkDem > 1)
            {
                MessageBox.Show("Chỉ Được Chọn 1 Khu Vực Để Tiến Hành Sửa Đổi!");
                return;
            }
            foreach (DataGridViewRow singleRow in gridViewXemDanhSachKhuVuc.Rows)
            {
                if (singleRow.Cells[3].Value != null)
                {
                    if ((Boolean)singleRow.Cells[3].Value == true)
                    {
                        ID = Convert.ToInt32(singleRow.Cells[1].Value.ToString());
                        Ten_Khu_Vuc = singleRow.Cells[2].Value.ToString(); 
                    }
                }
            }
            FormCapNhatKhuVuc f = new FormCapNhatKhuVuc();
            f.ShowDialog();
        }
    }
}
